﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;



namespace TimeReporter
{
    
    public class folderReader
    {
                
        public static void populateLists(TaskList taskList, ProjectList projList)
        {
            //Verkar inte som om denna används!!
            
            FileInfo[] theFiles = getFilesInFolder(projList.PathToFiles);

            foreach (FileInfo fi in theFiles)
            {
                projList.Add(new ProjectObject(fi));

                //int currentProjectID = projList.currentObject().ProjectID;
                List<TaskObject> temp = new List<TaskObject>();

                temp = projectData(fi);

                foreach (TaskObject t in temp)
                {
                    //t.ProjectID = currentProjectID;
                    taskList.Add(t);
                }
            }
            projList.IsFilled = true;
        }

        public static void populateSingleList(ProjectList projList)
        {
            FileInfo[] theFiles = getFilesInFolder(projList.PathToFiles);

            foreach (FileInfo fi in theFiles)
            {
                ProjectObject temp = new ProjectObject(fi);
                //int currentProjectID = projList.currentObject().ProjectID;
                List<TaskObject> temp2 = new List<TaskObject>();
                temp2 = projectData(fi);

                foreach (TaskObject t in temp2)
                {
                   // t.ProjectID = currentProjectID;
                    temp.theTasks.Add(t);
                }
                projList.Add(temp);
            }
            projList.IsFilled = true;
        }
        
        
        private static FileInfo[] getFilesInFolder(string pathToFolder)
        {
            DirectoryInfo theFolder = new DirectoryInfo(pathToFolder);
            FileInfo[] theFiles = theFolder.GetFiles("*.csv");
            return theFiles;
        }

        private static List<TaskObject> projectData(FileInfo fi)
        {
            FileStream aFile = new FileStream(fi.FullName.ToString(), FileMode.Open);
            StreamReader sr = new StreamReader(aFile);
            string strLine;
            List<TaskObject> temp = new List<TaskObject>();
            

            //Read past header line
            strLine = sr.ReadLine();
            //Read first data row
            strLine = sr.ReadLine();

            while (strLine != null)
            {
                temp.Add(new TaskObject(strLine));

                strLine = sr.ReadLine();
            }

            sr.Close();

            return temp;
        }
    }
}
